package com.contactstranslate.activity;



import java.math.BigDecimal;

import org.json.JSONException;

import com.contactstranslate.R;
import com.contactstranslate.utils.CTUtils;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;

import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;



public class CTPayPalActivity extends ActionBarActivity implements OnClickListener{


	private SeekBar seekBar;
	private TextView textView;
	private int cout;
	private int nombreDeContact;	

	private static PayPalConfiguration config = new PayPalConfiguration()
	.environment(PayPalConfiguration.ENVIRONMENT_NO_NETWORK)
	.clientId("APP-80W284485P519543T")
	.acceptCreditCards(true);

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ctpay_pal);
		String strCredit = CTUtils.getPreference(CTUtils.ARG_CREDIT, this);
		int credit = Integer.parseInt(strCredit);
		((TextView) findViewById(R.id.tn_cagnote_header)).setText(String.format(getString(R.string.tn_cagnote_header_paypal),   credit, getString(R.string.tn_paiement_paypal)));
		seekBar = (SeekBar) findViewById(R.id.ct_paiement_paypal_seekBar);
		seekBar.setProgress(500);
		textView = (TextView) findViewById(R.id.ct_paiement_paypal_seekBar_value);
		textView.setText(String.format(getString(R.string.ct_paiement_paypal_seekBar_value),  seekBar.getProgress() ,CTUtils.calculCout(500, this)));

		cout = 330;
		nombreDeContact = 500;
		seekBar.setEnabled(false);
		seekBar.setOnSeekBarChangeListener(new 
				OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {	
				int progress = seekBar.getProgress();
				if(seekBar.getProgress() <= 500){
					cout = CTUtils.calculCout(500, CTPayPalActivity.this);

					seekBar.setProgress(500);
					textView.setText(String.format(getString(R.string.ct_paiement_paypal_seekBar_value),  500 ,cout));
					nombreDeContact = 500;
				}else{

					cout = CTUtils.calculCout(progress, CTPayPalActivity.this);
					Log.e("Utilsprogress ", progress+" = "+cout);
					textView.setText(String.format(getString(R.string.ct_paiement_paypal_seekBar_value),  seekBar.getProgress() ,cout));
					nombreDeContact = progress;
				}			
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {				
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				Log.e("ZAU", fromUser+"");
				cout = CTUtils.calculCout(progress, CTPayPalActivity.this);
				textView.setText(String.format(getString(R.string.ct_paiement_paypal_seekBar_value),  seekBar.getProgress() ,cout));
				nombreDeContact = CTUtils.getMajor(progress, CTPayPalActivity.this);
				seekBar.setProgress(nombreDeContact);

			}
		});
		RadioButton radioButton1 = (RadioButton) findViewById(R.id.ct_cagnote1);
		radioButton1.setText(
				String.format(getString(R.string.ct_cagnote1),
						Integer.parseInt(getResources().getStringArray(R.array.ct_product_cost)[0])));

		RadioButton radioButton2 = (RadioButton) findViewById(R.id.ct_cagnote2);
		radioButton2.setText(
				String.format(getString(R.string.ct_cagnote2),
						Integer.parseInt(getResources().getStringArray(R.array.ct_product_cost)[1])));

		radioButton1.setOnClickListener(this);
		radioButton2.setOnClickListener(this);

		 findViewById(R.id.ct_paiement_paypal_button).setOnClickListener(this);
		 findViewById(R.id.ct_pay_pal_personnaliser).setOnClickListener(this);

		//initialisation du service de paiement PayPal
		Intent intent = new Intent(this, PayPalService.class);
		intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
		startService(intent);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ctpay_pal, menu);
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return false;
		}
		if(id ==android.R.id.home){

			Intent upIntent = NavUtils.getParentActivityIntent(this);
			upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
			NavUtils.navigateUpTo(this, upIntent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
			if (confirm != null) {
				try {
					Log.i(CTPayPalActivity.class.getName(), confirm.toJSONObject().toString(4));

					String strCredit = CTUtils.getPreference(CTUtils.ARG_CREDIT, this);
					int credit = Integer.parseInt(strCredit);
					credit +=nombreDeContact;
					CTUtils.savePreference(CTUtils.ARG_CREDIT, credit+"", this);
					((TextView) findViewById(R.id.tn_cagnote_header)).setText(String.format(getString(R.string.tn_cagnote_header_paypal),   credit, getString(R.string.tn_paiement_paypal)));
				} catch (JSONException e) {
					Log.e(CTPayPalActivity.class.getName(), "an extremely unlikely failure occurred: ", e);
				}
			}
		}
		else if (resultCode == Activity.RESULT_CANCELED) {
			Log.i(CTPayPalActivity.class.getName(), "The user canceled.");
		}
		else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
			Log.i(CTPayPalActivity.class.getName(), "An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ct_paiement_paypal_button:

			PayPalPayment payment = new PayPalPayment(new BigDecimal(cout / CTUtils.DEFAULT_CURRENCY_AMOUNT), 
					CTUtils.DEFAULT_CURRENCY_CODE, 
					nombreDeContact+" Contacts.",
					PayPalPayment.PAYMENT_INTENT_SALE);

			Intent intent = new Intent(CTPayPalActivity.this, PaymentActivity.class);
			intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
			startActivityForResult(intent, 0);
			break;
		case R.id.ct_cagnote1:
			nombreDeContact = 500;
			seekBar.setProgress(nombreDeContact);			
			seekBar.setEnabled(((RadioButton)findViewById(R.id.ct_pay_pal_personnaliser)).isChecked());
			break;
		case R.id.ct_cagnote2:
			nombreDeContact = 1000;
			seekBar.setProgress(nombreDeContact);			
			seekBar.setEnabled(((RadioButton)findViewById(R.id.ct_pay_pal_personnaliser)).isChecked());
			break;
		case R.id.ct_pay_pal_personnaliser:
			
			seekBar.setEnabled(((RadioButton)findViewById(R.id.ct_pay_pal_personnaliser)).isChecked());

			break;
		default:
			break;
		}


	}


}
